package com.zcxx.cotton.pick;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.zcxx.cotton.pick.domain.PickMapInfo;
import com.zcxx.cotton.pick.service.IPickMapInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Year;
import java.util.List;


/**
 * 地图信息Controller
 * @author ruoyi
 * @date 2021-05-06
 */
@RestController
@RequestMapping("/pick/mapInfo")
public class PickMapInfoController extends BaseController
{


    @Autowired
    private IPickMapInfoService pickMapInfoService;



    /**
     * 查询地图信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PickMapInfo pickMapInfo)
    {
        startPage();
        List<PickMapInfo> list = pickMapInfoService.selectPickMapInfoList(pickMapInfo);
        return getDataTable(list);
    }

    /**
     * 导出地图信息列表
     */
    @PostMapping("/export")
    public AjaxResult export(PickMapInfo pickMapInfo)
    {
        Year year = Year.now();
        String tbName= "pick_map_info";
        if(pickMapInfo.getDeviceMainCode()!= null) {
            tbName = "pick_map_info" + pickMapInfo.getDeviceMainCode() + year.toString();
        }
        pickMapInfo.setTbName(tbName);
        List<PickMapInfo> list = pickMapInfoService.selectPickMapInfoList(pickMapInfo);
        ExcelUtil<PickMapInfo> util = new ExcelUtil<PickMapInfo>(PickMapInfo.class);
        return util.exportExcel(list, "mapInfo");
    }


    /**
     * 新增保存地图信息
     */
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody PickMapInfo pickMapInfo)
    {
        return toAjax(pickMapInfoService.insertPickMapInfo(pickMapInfo));
    }

    /**
     * 修改地图信息
     */
    @GetMapping("/edit/{mapInfoId}")
    public AjaxResult edit(@PathVariable("mapInfoId") Long mapInfoId)
    {
        PickMapInfo pickMapInfo = pickMapInfoService.selectPickMapInfoById(mapInfoId);
        return AjaxResult.success(pickMapInfo);

    }

    /**
     * 修改保存地图信息
     */
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody PickMapInfo pickMapInfo)
    {
        return toAjax(pickMapInfoService.updatePickMapInfo(pickMapInfo));
    }

    /**
     * 删除地图信息
     */
    @PostMapping( "/remove")
    public AjaxResult remove(String ids)
    {
        return toAjax(pickMapInfoService.deletePickMapInfoByIds(ids));
    }
}
